'use strict';

const APP_ENV = process.env.APP_ENV || 'pre';
const apis = require('../src/model/endpoints');
const path = require('path');

const plugins = [
    'preval',
    'transform-class-properties',
    'transform-object-rest-spread',
    ['transform-decorators-legacy']
];

var config = {
    alias: {
        '@doctorwork/utils': path.resolve(
            __dirname,
            '../src/model/utils/weapp'
        ),
        '@': path.resolve(__dirname, '..', 'src/')
    },
    projectName: 'health-weapp',
    date: '2018-7-12',
    designWidth: 750,
    sourceRoot: 'src',
    outputRoot: 'dist',
    env: {
        appName: process.env.APP_NAME || 'health-weapp',
        appId: process.env.APP_ID || '"wxc35f6af43f92df8c"',
        APP_ENV,
        ...apis,
        DEBUG: process.env.DEBUG
    },
    plugins: {
        babel: {
            sourceMap: true,
            presets: ['env'],
            plugins
        },
        typescript: {
            compilerOptions: {
                allowSyntheticDefaultImports: true,
                baseUrl: '.',
                declaration: false,
                experimentalDecorators: true,
                jsx: 'react',
                jsxFactory: 'Nerv.createElement',
                module: 'commonjs',
                moduleResolution: 'node',
                noImplicitAny: false,
                noUnusedLocals: true,
                outDir: './dist/',
                preserveConstEnums: true,
                removeComments: false,
                rootDir: '.',
                sourceMap: true,
                strictNullChecks: true,
                target: 'es6'
            },
            include: ['src/**/*'],
            exclude: ['node_modules'],
            compileOnSave: false
        },
        less: {
            paths: [path.resolve(__dirname, '../src/asset/style')]
        }
    },
    weapp: {
        module: {
            postcss: {}
        }
    },
    h5: {
        publicPath: '/',
        staticDirectory: 'static',
        module: {
            postcss: {
                autoprefixer: {
                    enable: true
                }
            }
        }
    }
};

module.exports = (merge, options = {}) => {
    // COMPILER SPEC OVERRIDE
    if (process.env.COMPILER_TYPE == 'h5') {
        Object.assign(options, require('./other/config'));
    }

    if (
        process.env.NODE_ENV === 'development' ||
        process.env.NODE_ENV === 'test'
    ) {
        return merge({}, config, require('./dev'), options);
    }
    return merge({}, config, require('./prod'), options);
};

exports.config = config;
